<template>
    <view class="bg-content">
        <u-sticky>
            <div style="height: 84px; border-bottom: 1rpx solid #eeeeee; background-color: #FFFFFF;">
                <u-search placeholder="订单号" v-model="searchKeyword" @search="onSearch" @custom="onSearch"></u-search>
                <u-tabs :list="tabList" name="title" :active-color="'red'" :is-scroll="false" :current="activeTabIndex" @change="onTabChange"></u-tabs>
            </div>
        </u-sticky>

        <scroll-view scroll-x="true">
            <div class="table table-bordered" style="width: 1500px; text-align: center; background: white;">
                <div class="tr">
                    <div class="td text-center bold" style="width: 100px;">回款状态</div>
                    <div class="td text-center bold" style="width: 100px;">订单号</div>
                    <div class="td text-center bold" style="width: 100px;">订单支付金额（单位/元）</div>
                    <div class="td text-center bold" style="width: 150px;">商户回款单号</div>
                    <div class="td text-center bold" style="width: 150px;">回款流水号</div>
                    <div class="td text-center bold" style="width: 150px;">回款方支付宝账户</div>
                    <div class="td text-center bold" style="width: 100px;">回款方支付宝真实姓名</div>
                    <div class="td text-center bold" style="width: 100px;">回款金额（单位/元）</div>
                    <div class="td text-center bold" style="width: 100px;">回款时间</div>
                    <div class="td text-center bold" style="width: 150px;">回款备注</div>
                    <div class="td text-center bold" style="width: 100px;">回款日志</div>
                    <div class="td text-center bold" style="width: 100px;">扣除交易手续费（单位/元）</div>
                    <div class="td text-center bold" style="width: 100px;">操作</div>
                </div>
                <div class="tr" v-for="(item, index)  in tbOrderList" :key="item.id">
                    <div class="td" style="width: 100px;">{{item.transferStatusText}}</div>
                    <div class="td" style="width: 100px;">{{item.code}}</div>
                    <div class="td" style="width: 100px;">{{item.payMoney | toFixed2}}</div>
                    <div class="td" style="width: 150px;">{{item.transferOutBizNo | toEmptyStr}}</div>
                    <div class="td" style="width: 150px;">{{item.transferTradeNo | toEmptyStr}}</div>
                    <div class="td" style="width: 150px;">{{item.transferPayeeAccount | toEmptyStr}}</div>
                    <div class="td" style="width: 100px;">{{item.transferPayeeRealName | toEmptyStr}}</div>
                    <div class="td" style="width: 100px;">{{item.transferAmount | toFixed2}}</div>
                    <div class="td" style="width: 100px;">{{item.transferDatetime | toEmptyStr}}</div>
                    <div class="td" style="width: 150px;">{{item.transferRemark | toEmptyStr}}</div>
                    <div class="td" style="width: 100px;">{{item.transferLog | toEmptyStr}}</div>
                    <div class="td" style="width: 100px;">{{(item.payMoney - item.transferAmount) | toFixed2}}</div>
                    <div class="td" style="width: 100px;">
                        <div style="text-decoration: underline; padding: 5px;" v-if="'0,2'.indexOf(item.transferStatus) !== -1" @click="transferMoney(item)">
                            手动回款
                        </div>
                    </div>
                </div>
            </div>
        </scroll-view>

        <div style="padding-top: 10px; padding-bottom: 10px;">
            <u-loadmore :status="pagination.status" bg-color="#eeeeee"/>
        </div>
    </view>
</template>

<script>

    import {TbOrderService} from "@/dev/services/tb-order.service";
    import {CommonUtil} from "@/dev/core/utils/common.util";

    export default {
        data() {
            return {
                searchKeyword: '',
                tbOrderList: [],
                validateForm: {
                    value: {
                        code: '',
                        transferOutBizNo: '',
                        transferTradeNo: '',
                        transferPayeeAccount: '',
                    },
                },
                tabList: [
                    {title: '全部', transferStatus: null},
                    {title: '回款成功', transferStatus: '1'},
                    {title: '回款失败', transferStatus: '2'},
                ],
                activeTabIndex: -1,
            }
        },
        onShow() {
            const _self = this;
            this.activeTabIndex = parseInt(this.urlParams.activeTabIndex)
            this.$forceUpdate();
        },
        watch: {
            activeTabIndex: {
                deep: true, // 深度监听
                handler(newVal, oldVal) {
                    console.log("activeTabIndex参数改变，即将刷新...", `新值：${newVal}`, `旧值：${oldVal}`)
                    this.doRefresh(this.tbOrderList)
                }
            }
        },
        // 上拉加载
        onReachBottom: function () {
            if (this.pagination.hasNextPage) {
                this.doInfinite()
            }
        },
        methods: {
            onSearch() {
                const _self = this;
                this.searchKeyword = CommonUtil.trim(this.searchKeyword)
                _self.doRefresh(_self.tbOrderList)
            },
            onTabChange(index) {
                const _self = this;
                this.activeTabIndex = index;
                this.urlParams.activeTabIndex = this.activeTabIndex;
            },
            /**
             * 加载更多数据中
             * @param infiniteScroll
             */
            doInfinite(infiniteScroll) {
                const _self = this;
                new TbOrderService().findTableListCascade({
                    pagination: this.pagination,
                    data: Object.assign({
                        storeId: _self.globalVariable.userInfo.tbStore.id,
                        status: '4',
                        transferStatus: _self.tabList[_self.activeTabIndex].transferStatus,
                        searchKeyword: _self.searchKeyword,
                        orderBy: 'last_update_time desc'
                    }, this.validateForm.value)
                }).then(result => {
                    console.log(_self.tbOrderList)
                    _self.pagination.refreshPage(result);
                    _self.tbOrderList = [..._self.tbOrderList, ...result.list]
                })
            },
            /**
             * 手动回款
             * @param item
             */
            transferMoney(item) {
                const _self = this;
                CommonUtil.alert({
                    title: '提示！',
                    content: '确认手动回款吗？',
                    showCancel: true, // 是否显示取消按钮
                    success: function (res) {
                        if (res.cancel) { // 点击取消,默认隐藏弹框
                        } else { // 点击确定
                            new TbOrderService().transferMoneyToSeller({id: item.id}).then(result => {
                                if (result.status === "success") {
                                    CommonUtil.alert(result.message);
                                } else {
                                    CommonUtil.alert(result.message);
                                }
                                _self.doRefresh(_self.tbOrderList)
                            });
                        }
                    }
                });
            }
        }
    }
</script>

<style lang="scss" scoped>
</style>
